package com.doudou.casebook;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.doudou.casebook.adapter.RecordPagerAdapter;
import com.doudou.casebook.frag_record.InComeFragment;
import com.doudou.casebook.frag_record.OutComeFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        //1.查找控件
        tabLayout = findViewById(R.id.record_tabs);
        viewPager = findViewById(R.id.record_vp);
        //2.设置ViewPage加载页面
        initPager();
    }

    private void initPager() {
        //初始化ViewPager页面的集合
        List<Fragment>fragmentList = new ArrayList<>();
        //创建收入和支出页面,放置在Fragment中
        OutComeFragment outComeFragment = new OutComeFragment();//支出
        InComeFragment inComeFragment = new InComeFragment();//收入
        fragmentList.add(outComeFragment);
        fragmentList.add(inComeFragment);


        //创建适配器
        RecordPagerAdapter pagerAdapter = new RecordPagerAdapter(getSupportFragmentManager(), fragmentList);
        //设置适配器
        viewPager.setAdapter(pagerAdapter);
        //将TableLayout和ViewPager进行关联

        tabLayout.setupWithViewPager(viewPager);
    }

    //点击事件
    public void onClick(View view) {
        //返回按钮
        if (view.getId() == R.id.record_iv_back) {
            finish();
        }
    }
}